package by.epam.trenings.vkphotorest.app;

import by.epam.trenings.vkphotorest.R;
import by.epam.trenings.vkphotorest.adapters.CursorFragmentPagerAdapter;
import by.epam.trenings.vkphotorest.content.contract.TaskContract;
import android.os.Bundle;
import android.database.Cursor;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

public class FullScreenPhotoActivity extends FragmentActivity implements
		ViewPager.OnPageChangeListener, LoaderManager.LoaderCallbacks<Cursor> {

	private TextView mNumberTextView;
	private int mNumber;
	private int mAlbumCount;
	private static final int LOADER_ID = 0;
	private CursorFragmentPagerAdapter mAdapter;
	private ViewPager viewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_full_screen_photo);

		Bundle extras = getIntent().getExtras();

		mNumber = extras.getInt(PhotoGridActivity.EXTRA_PHOTO_POSITION);

		viewPager = (ViewPager) findViewById(R.id.activity_full_screen_view_pager);

		mAdapter = new CursorFragmentPagerAdapter(
				FullScreenPhotoActivity.this, getSupportFragmentManager(), null);
		viewPager.setAdapter(mAdapter);

		getSupportLoaderManager().initLoader(LOADER_ID, null,
				FullScreenPhotoActivity.this);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	/**
	 * This method will be invoked when the current page is scrolled, either as
	 * part of a programmatically initiated smooth scroll or a user initiated
	 * touch scroll.
	 */
	@Override
	public void onPageScrolled(int position, float positionOffset,
			int positionOffsetPixels) {
	}

	/** Called when the scroll state changes. */
	@Override
	public void onPageScrollStateChanged(int state) {

	}

	/** This method will be invoked when a new page becomes selected. */
	@Override
	public void onPageSelected(int page) {
		mNumberTextView = (TextView) findViewById(R.id.activity_full_screen_number);
		int realPage = page + 1;
		String from = getResources().getString(
				R.string.activity_full_screen_strings_from);
		mNumberTextView.setText(realPage + from + mAlbumCount);
	}

	@Override
	public Loader<Cursor> onCreateLoader(int paramInt, Bundle paramBundle) {
		return new CursorLoader(this, TaskContract.PhotosColumns.CONTENT_URI,
				null, null, null, null);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> paramLoader, final Cursor cursor) {
		if (paramLoader.getId() == LOADER_ID) {

			mAdapter.swapCursor(cursor);
			viewPager.setCurrentItem(mNumber);
			viewPager.setOnPageChangeListener(this);

			mAlbumCount = cursor.getCount();
			mNumberTextView = (TextView) findViewById(R.id.activity_full_screen_number);

			int realPage = mNumber + 1;
			String from = getResources().getString(
					R.string.activity_full_screen_strings_from);
			mNumberTextView.setText(realPage + from + mAlbumCount);
		}

	}

	@Override
	public void onLoaderReset(Loader<Cursor> paramLoader) {
		mAdapter.swapCursor(null);
	}
}
